using System;
using System.Collections.Generic;
using System.Text;
using System.Net;

namespace ShareTools.GroupsService
{
    public class GroupsServiceManager
    {
        string userName;

        public string UserName
        {
            get { return userName; }
            set { userName = value; }
        }
        string password;

        public string Password
        {
            get { return password; }
            set { password = value; }
        }
        string domainName;

        public string DomainName
        {
            get { return domainName; }
            set { domainName = value; }
        }
        string WebServiceURL;

        public string WebServiceURI
        {
            get { return WebServiceURL; }
            set { WebServiceURL = value; }
        }

        ugWebService.UserGroup ugws = null;

        private List<string> groupExclusions;

        public List<string> GroupExclusions
        {
            get { return groupExclusions; }
            set { groupExclusions = value; }
        }


        private ugWebService.UserGroup GetWebserviceConnection()
        {
            
            try
            {
                if (ugws == null)
                {
                    ugws = new ugWebService.UserGroup();
                    NetworkCredential nc = new NetworkCredential(UserName, Password, DomainName);
                    ugws.Credentials = nc;
                    ugws.Url = WebServiceURI;
                }
            }
            catch (Exception ex)
            {
            }

            return ugws;
        }

        public string EnlistGroups()
        {
            return DeleteGroups(false);
        }
        public string DeleteGroups()
        {
            return DeleteGroups(true);
        }
        private string DeleteGroups(bool deleteGroups)
        {
            ugWebService.UserGroup ugws = GetWebserviceConnection();
            StringBuilder sb = new StringBuilder();

            System.Xml.XmlNode ndGroup = ugws.GetGroupCollectionFromSite();
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.LoadXml(ndGroup.InnerXml);

            System.Xml.XmlElement root = doc.DocumentElement;
            System.Xml.XmlNodeList lst = root.GetElementsByTagName("Group");

            sb.Append("Total groups found:" + lst.Count).Append(Environment.NewLine);
            string name = string.Empty;
            foreach (System.Xml.XmlNode n in lst)
            {
                try
                {
                    name = n.Attributes[1].Value;
                    if (deleteGroups && GroupExclusions != null && GroupExclusions.Count > 0 && !GroupExclusions.Contains(name))
                    {
                        ugws.RemoveGroup(name);
                        sb.Append("Successfully deleted:" + name);
                    }
                    else
                    //if(!deleteGroups)
                        sb.Append(name);
                }
                catch (Exception ex)
                {
                    sb.Append("Failed to delete:" + name);
                }
                sb.Append(Environment.NewLine);
            }//for each
            return sb.ToString();
        }
    }


}
